package com.ysstech.reportworld.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSONArray;
import com.ysstech.common.entity.ColumnInfo;
import com.ysstech.common.entity.TableInfo;
import com.ysstech.common.enums.YssDataBaseEnum;
import com.ysstech.common.util.DateUtil;
import com.ysstech.common.util.JdbcUtil;
import com.ysstech.common.util.StringUtil;
import com.ysstech.reportworld.entity.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.CollectionUtils;

import javax.json.JsonArray;
import java.sql.*;
import java.util.*;

@Slf4j
public class TableInfoUtil {

    private void readTableInfo(DbConnConfig dbConnConfig) throws Exception {
        ExcelWriter writer = null;
        ExcelWriter writer2 = null;
        try {
            String[] arrType = new String[]{"现金管理产品", "资管业务季度监测表", "非法人报表", "央行[2018]299号", "OPSRR022-偿二代", "FISP002", "FISP003", "FISP004"};
            List<Map<String, Object>> list = new ArrayList<>();
            List<Map<String, Object>> list2 = new ArrayList<>();
            Map<String, String> stringMap = new HashMap<>();
            for (String ftype : arrType) {
                Map<TableInfo, List<ColumnInfo>> map = JdbcUtil.getTableInfoMap(this.getTableInfo(ftype, dbConnConfig.getInputConnection()), dbConnConfig.getInputConnection());
                if (CollectionUtils.isEmpty(map)) {
                    continue;
                }
                for (Map.Entry<TableInfo, List<ColumnInfo>> entry : map.entrySet()) {
                    TableInfo tableInfo = entry.getKey();
                    if (null == tableInfo) {
                        continue;
                    }
                    Map<String, Object> row = new LinkedHashMap<>();
                    row.put("分类", tableInfo.getKeyName());
                    row.put("报表名称", tableInfo.getReportName());
                    row.put("表名", tableInfo.getName());
                    row.put("表中文名称", tableInfo.getRemarks());
                    list.add(row);
                    if (stringMap.containsKey(tableInfo.getName())) {
                        continue;
                    }
                    if (CollectionUtils.isEmpty(entry.getValue())) {
                        log.error(tableInfo.getName() + "没有获取到对应的字段信息!");
                        continue;
                    }
                    for (ColumnInfo columnInfo : entry.getValue()) {
                        if (null == columnInfo) {
                            continue;
                        }
                        Map<String, Object> row2 = new LinkedHashMap<>();
                        row2.put("中文表名", tableInfo.getRemarks());
                        row2.put("英文表名", tableInfo.getName());
                        row2.put("字段名称", columnInfo.getName());
                        if (3 == columnInfo.getDataType()) { // NUMBER
                            row2.put("字段类型", columnInfo.getDataTypeName() + "(" + columnInfo.getPrecision() + "," + columnInfo.getScale() + ")");
                        } else {
                            row2.put("字段类型", columnInfo.getDataTypeName() + "(" + columnInfo.getPrecision() + ")");
                        }

                        row2.put("可为空", columnInfo.getIsNull());
                        row2.put("默认值", columnInfo.getDefaultValue());
                        row2.put("字段中文名", columnInfo.getRemarks());
                        row2.put("字典码", columnInfo.getDicInfo());
                        list2.add(row2);
                    }
                    stringMap.put(tableInfo.getName(), tableInfo.getName());
                }
            }
            if (!CollectionUtils.isEmpty(list)) {
                ArrayList<Map<String, Object>> rows = CollUtil.newArrayList(list);
                writer = ExcelUtil.getWriter("D:\\table\\表信息.xls");
                String title = "表信息";
                // 合并单元格后的标题行，使用默认标题样式
                if (StringUtils.isNotBlank(title)) {
                    writer.merge(list.get(0).size() - 1, title);
                }
                if (rows != null && rows.size() > 0) {
                    writer.write(rows);
                }
            }
            if (!CollectionUtils.isEmpty(list2)) {
                ArrayList<Map<String, Object>> rows = CollUtil.newArrayList(list2);
                writer2 = ExcelUtil.getWriter("D:\\table\\表字段信息.xls");
                String title = "表字段信息";
                // 合并单元格后的标题行，使用默认标题样式
                if (StringUtils.isNotBlank(title)) {
                    writer2.merge(list2.get(0).size() - 1, title);
                }
                if (rows != null && rows.size() > 0) {
                    writer2.write(rows);
                }
            }
            log.info("生成成功！表" + list.size() + "张！");
        } catch (Exception ex) {
            log.error(ex.getMessage());
        } finally {
            if (null != writer) {
                writer.close();
            }
            if (null != writer2) {
                writer2.close();
            }
        }
    }

    public List<TableInfo> getTableInfo(String type, Connection conn) throws Exception {
        ResultSet rs = null;
        PreparedStatement pStemtQuery = null;
        List<TableInfo> list = new ArrayList<>();
        try {
            String sql = " select c.key_name, a.dataset_name,a.report_id, b.report_name, a.dataset_sql,b.report_tablename" +
                    "    from tc_rep_dataset a" +
                    "   inner join tc_rep_para b on a.report_id = b.report_id" +
                    "   inner join tp_gl_exhib_datadict c on b.regulation_rule = key_id" +
                    "   where class_id = 'A00002'" +
                    "   and c.key_name = '" + type + "'";
            pStemtQuery = conn.prepareStatement(sql);
            rs = pStemtQuery.executeQuery();
            while (rs.next()) {
                // sql语句
                String dataset_sql = rs.getString("dataset_sql");
                // 监管规则
                String key_name = rs.getString("key_name");
                // 报表名称
                String dataset_name = rs.getString("dataset_name");
                dataset_sql = dataset_sql.replace("${PFID}", "='${PFID}'");
                dataset_sql = dataset_sql.replace("round(WRJSHZB ,2) as", "round(WRJSHZB ,2) as WRJSHZB");
                dataset_sql = dataset_sql.replace("<CLOB>", "");
                if ("1".equals(dataset_sql)) {
                    continue;
                }
                dataset_sql = dataset_sql.replace("AND AND", "AND");
                if (StringUtil.oneCase(key_name + rs.getString("dataset_name")
                        , "央行[2018]299号表3-3资产负债剩余期限,央行[2018]299号募集余额明细（持有人维度）")) {
                    dataset_sql += ")";
                }
                if ("场外期权投资明细".equals(rs.getString("dataset_name"))) {
                    dataset_sql = dataset_sql.substring(0, dataset_sql.lastIndexOf(")"));
                }
                TableInfo tableInfo = new TableInfo();
                tableInfo.setKeyName(key_name);
                tableInfo.setReportName(dataset_name);
                tableInfo.setSql(dataset_sql);
                tableInfo.setReportId(rs.getString("report_id"));
                //报表落地表
                tableInfo.setReportTablename(rs.getString("report_tablename"));
                if (StringUtils.isNotBlank(dataset_sql)) {
                    list.add(tableInfo);
                } else {
                    log.error("没有取到sql语句的监管规则和报表====" + key_name + dataset_name);
                }
            }
        } catch (Exception ex) {
            log.error("通过监管规则下的语句取表信息出错" + ex.getMessage());
        } finally {
            JdbcUtil.close(rs, pStemtQuery, conn);
        }
        return list;
    }


    /**
     * 数据库的类型转成报表世界需要的表字段信息
     *
     * @param columnInfo
     * @return
     * @throws Exception
     */
    public static TableDetail getReportTableDetail(ColumnInfo columnInfo, String tableId) throws Exception {
        TableDetail tableDetail = new TableDetail();
        if (StringUtils.isNotBlank(tableId)) {
            tableDetail.setId(DateUtil.generateUUID());
            tableDetail.setTableId(tableId);
        }
        tableDetail.setColumnName(columnInfo.getName());
        tableDetail.setColumnChineseName(columnInfo.getRemarks());
        tableDetail.setColumnNull("2".equals(columnInfo.getIsNull()) ? 2 : 1);
        tableDetail.setColumnDefault(columnInfo.getDefaultValue());
        tableDetail.setPrimaryKey(columnInfo.getPrimaryKey());
        //tableDetail.setIndexed(columnInfo.getIndexed());
        //tableDetail.setIndexName(columnInfo.getIndexName());
        if (StringUtils.isNotBlank(columnInfo.getDicInfo())) {
            tableDetail.setCodeName(columnInfo.getDicInfo());
        }
        if (StringUtil.oneCase(columnInfo.getDataTypeName(), "VARCHAR2,VARCHAR")) {
            tableDetail.setColumnType(1);
            tableDetail.setColumnLength(columnInfo.getPrecision());
        }
        if ("NUMBER".equals(columnInfo.getDataTypeName())) {
            tableDetail.setColumnType(2);
            tableDetail.setColumnLength(columnInfo.getPrecision());
            if (0 != columnInfo.getScale()) { //小数
                tableDetail.setColumnType(3);
                tableDetail.setColumnDecimal(columnInfo.getScale());
                // 针对 NUMBER（*）的
                if (0 == columnInfo.getPrecision() && -127 == columnInfo.getScale()) {
                    tableDetail.setColumnDecimal(0);
                    tableDetail.setColumnLength(0);
                    tableDetail.setColumnType(2);
                }
            }
        }
        if ("CLOB".equals(columnInfo.getDataTypeName())) {
            tableDetail.setColumnType(4);
            tableDetail.setColumnLength(0);
        }
        if (StringUtil.oneOf(columnInfo.getDataTypeName(), "TIMESTAMP")) {
            tableDetail.setColumnType(5);
            tableDetail.setColumnLength(columnInfo.getPrecision());
            // tableDetail.setColumnLength(columnInfo.getScale());
        }
        if ("DATE".equals(columnInfo.getDataTypeName())) {
            tableDetail.setColumnType(6);
            tableDetail.setColumnLength(0);
        }
        if ("BLOB".equals(columnInfo.getDataTypeName())) { //BLOB
            tableDetail.setColumnType(7);
            tableDetail.setColumnLength(0);
        }
        if ("NCLOB".equals(columnInfo.getDataTypeName())) {
            tableDetail.setColumnType(8);
            tableDetail.setColumnLength(0);
        }
        if ("NVARCHAR2".equals(columnInfo.getDataTypeName())) {
            tableDetail.setColumnType(9);
            tableDetail.setColumnLength(columnInfo.getPrecision());
        }
        if ("CHAR".equals(columnInfo.getDataTypeName())) {
            tableDetail.setColumnType(10);
            tableDetail.setColumnLength(columnInfo.getPrecision());
        }
        if ("INTEGER".equals(columnInfo.getDataTypeName())) {
            tableDetail.setColumnType(11);
            tableDetail.setColumnLength(columnInfo.getPrecision());
        }
        if ("FLOAT".equals(columnInfo.getDataTypeName())) {
            tableDetail.setColumnType(12);
            tableDetail.setColumnLength(0);
        }
        return tableDetail;
    }

    /**
     * 通过字段类型返回
     *
     * @param tableDetail
     * @param length
     * @return
     */
    public static String handleCloum(TableDetail tableDetail, boolean length, String ftype) {
        YssDataBaseEnum baseEnum = YssDataBaseEnum.getEnum(ftype);
        switch (baseEnum) {
            case ORACLE:
                return handleCloum(tableDetail, true);
            case MYSQL:
                return handleCloumMysql(tableDetail, true);
            case DM:
                return handleCloum(tableDetail, true);
            default:
                return null;
        }
    }

    /**
     * 通过字段类型返回
     *
     * @param tableDetail
     * @param length
     * @return
     */
    public static String handleCloumMysql(TableDetail tableDetail, boolean length) {
        Integer columnType = tableDetail.getColumnType();
        switch (columnType) {
            case 1:
                if (tableDetail.getColumnLength() > 200 && tableDetail.getColumnLength() <= 256) {
                    return "TINYTEXT";
                }
                if (tableDetail.getColumnLength() >= 2000 && tableDetail.getColumnLength() <= 4000) {
                    return "TEXT";
                }
                return "VARCHAR" + (length ? "(" + tableDetail.getColumnLength() + ")" : "");
            case 2:
                return "DECIMAL" + (length && 0 != tableDetail.getColumnLength() ? "(" + tableDetail.getColumnLength() + ")" : "");
            case 3:
                return "DECIMAL" + (length && 0 != tableDetail.getColumnLength() && 0 != tableDetail.getColumnLength() ? "(" + tableDetail.getColumnLength() + "," + tableDetail.getColumnDecimal() + ")" : "");
            case 4:
                return "TEXT";
            case 5:
                return "DATETIME" + (length && 0 != tableDetail.getColumnLength() ? "(" + tableDetail.getColumnLength() + ")" : "");
            case 6:
                return "DATE" + (length && 0 != tableDetail.getColumnLength() ? "(" + tableDetail.getColumnLength() + ")" : "");
            case 7:
                return "TEXT";
            case 8:
                return "TEXT";
            case 9:
                if (tableDetail.getColumnLength() > 200 && tableDetail.getColumnLength() <= 256) {
                    return "TINYTEXT";
                }
                if (tableDetail.getColumnLength() >= 2000 && tableDetail.getColumnLength() <= 4000) {
                    return "TEXT";
                }
                return "VARCHAR" + (length && 0 != tableDetail.getColumnLength() ? "(" + tableDetail.getColumnLength() + ")" : "");
            case 10:
                if (tableDetail.getColumnLength() > 200 && tableDetail.getColumnLength() <= 256) {
                    return "TINYTEXT";
                }
                if (tableDetail.getColumnLength() >= 2000 && tableDetail.getColumnLength() <= 4000) {
                    return "TEXT";
                }
                return "VARCHAR" + (length && 0 != tableDetail.getColumnLength() ? "(" + tableDetail.getColumnLength() + ")" : "");
            case 11:
                return "INTEGER" + (length && 0 != tableDetail.getColumnLength() ? "(" + tableDetail.getColumnLength() + ")" : "");
            case 12:
                return "FLOAT";
            default:
                return null;
        }

    }

    /**
     * 通过字段类型返回
     *
     * @param tableDetail
     * @param length
     * @return
     */
    public static String handleCloum(TableDetail tableDetail, boolean length) {
        Integer columnType = tableDetail.getColumnType();
        switch (columnType) {
            case 1:
                return "VARCHAR2" + (length ? "(" + tableDetail.getColumnLength() + ")" : "");
            case 2:
                return "NUMBER" + (length && 0 != tableDetail.getColumnLength() ? "(" + tableDetail.getColumnLength() + ")" : "");
            case 3:
                return "NUMBER" + (length && 0 != tableDetail.getColumnLength() && 0 != tableDetail.getColumnLength() ? "(" + tableDetail.getColumnLength() + "," + tableDetail.getColumnDecimal() + ")" : "");
            case 4:
                return "CLOB";
            case 5:
                return "TIMESTAMP" + (length && 0 != tableDetail.getColumnLength() ? "(" + tableDetail.getColumnLength() + ")" : "");
            case 6:
                return "DATE" + (length && 0 != tableDetail.getColumnLength() ? "(" + tableDetail.getColumnLength() + ")" : "");
            case 7:
                return "BLOB";
            case 8:
                return "NCLOB";
            case 9:
                return "NVARCHAR2" + (length && 0 != tableDetail.getColumnLength() ? "(" + tableDetail.getColumnLength() + ")" : "");
            case 10:
                return "CHAR" + (length && 0 != tableDetail.getColumnLength() ? "(" + tableDetail.getColumnLength() + ")" : "");
            case 11:
                return "INTEGER" + (length && 0 != tableDetail.getColumnLength() ? "(" + tableDetail.getColumnLength() + ")" : "");
            case 12:
                return "FLOAT";
            default:
                return null;
        }

    }

    /**
     * 建表语句生成，区分数据库类型
     *
     * @param table
     * @param listDetail
     * @param ftype
     * @return
     * @throws Exception
     */
    public static String creatTable(Table table, List<TableDetail> listDetail, String ftype) throws Exception {
        YssDataBaseEnum baseEnum = YssDataBaseEnum.getEnum(ftype);
        switch (baseEnum) {
            case ORACLE:
                return creatTableOracle(table, listDetail);
            case MYSQL:
                return creatTableMysql(table, listDetail);
            case DM:
                return creatTableOracle(table, listDetail);
            default:
                return null;
        }
    }

    /**
     * 生成建表语句脚本 ORCLE
     *
     * @param table
     * @param listDetail
     * @return
     * @throws Exception
     */
    public static String creatTableOracle(Table table, List<TableDetail> listDetail) throws Exception {
        StringBuffer buffer = new StringBuffer();
        StringBuffer primarykey = new StringBuffer();
        StringBuffer comment = new StringBuffer();
        StringBuffer indexed = new StringBuffer();
        buffer.append("\r\n").append("CREATE TABLE ").append(table.getName()).append("\r\n(");
        String chineseName = "";
        if (StringUtils.isNotBlank(table.getChineseName())) {
            chineseName = "comment on table " + table.getName() + "  is '" + table.getChineseName() + "';\r\n";
        }
        int i = 0;
        for (TableDetail detail : listDetail) {
            i++;
            if (null == detail || StringUtils.isBlank(detail.getColumnName())) {
                continue;
            }
            buffer.append(detail.getColumnName()).append(" ").append(TableInfoUtil.handleCloum(detail, true));
            if (StringUtils.isNotBlank(detail.getColumnDefault())) {
                String defaultValue = getColumnDefault(detail.getColumnDefault(), YssDataBaseEnum.ORACLE.getCode());
                if (StringUtils.isNotBlank(defaultValue)) {
                    buffer.append(" DEFAULT  ").append(defaultValue);
                }
            }
            if (2 == detail.getColumnNull()) {
                buffer.append("  NOT NULL");
            }
            //主键
            if (1 == detail.getPrimaryKey()) {
                primarykey.append(detail.getColumnName()).append(",");
            }
            if (i == listDetail.size() && primarykey.length() < 2) {  //最后一条，表又没有主键的时候，不拼逗号
                buffer.append("\r\n");
            } else {
                buffer.append(",").append("\r\n");
            }
            // 字段描述
            if (StringUtils.isNotBlank(detail.getColumnChineseName())) {
                comment.append(" COMMENT ON COLUMN " + table.getName()).append(".")
                        .append(detail.getColumnName()).append(" IS ")
                        .append("'" + detail.getColumnChineseName() + /*(StringUtils.isNotBlank(detail.getColumnDescribe()) ? detail.getColumnDescribe() : "") +*/ "';")
                        .append("\r\n");
            }
            // 索引
         /*   if (1 == detail.getIndexed()) {
                if (indexedMap.containsKey(detail.getIndexName())) {
                    List<String> list = indexedMap.get(detail.getIndexName());
                    list.add(detail.getIndexColumnName());
                    indexedMap.put(detail.getIndexName(), list);
                } else {
                    List<String> list = new ArrayList<>();
                    list.add(detail.getIndexColumnName());
                    indexedMap.put(detail.getIndexName(), list);
                }
            }*/
        }
        if (primarykey.length() > 2) {
            buffer.append(" CONSTRAINT ")
                    .append(table.getName())
                    .append(" PRIMARY KEY (" + primarykey.deleteCharAt(primarykey.length() - 1) + ")")
                    .append("\r\n");
        }

        // 索引
        List<TableIndexed> indexedList = table.getListIndexed();
        if (!CollectionUtils.isEmpty(indexedList)) {
            Map<String, List<String>> indexedMap = new HashMap<>();
            for (TableIndexed tableIndexed : indexedList) {
                if (null == tableIndexed) {
                    continue;
                }
                List<String> list = null;
                if (indexedMap.containsKey(tableIndexed.getIndexedName())) {
                    list = indexedMap.get(tableIndexed.getIndexedName());
                    list.add(tableIndexed.getIndexedColumnName());
                } else {
                    list = new ArrayList<>();
                    list.add(tableIndexed.getIndexedColumnName());
                }
                indexedMap.put(tableIndexed.getIndexedName(), list);
            }
            if (!CollectionUtils.isEmpty(indexedMap)) {
                for (Map.Entry<String, List<String>> entry : indexedMap.entrySet()) {
                    if (StringUtils.isBlank(entry.getKey())) {
                        continue;
                    }
                    List<String> list = entry.getValue();
                    if (CollectionUtils.isEmpty(list)) {
                        continue;
                    }
                    indexed.append(" CREATE INDEX " + entry.getKey()).append(" ON ").append(table.getName()).append("(");
                    StringBuffer indexedCol = new StringBuffer();
                    for (String columnName : list) {
                        indexedCol.append(columnName).append(",");
                    }
                    indexed.append(indexedCol.deleteCharAt(indexedCol.length() - 1)).append(")").append(";").append("\r\n");
                }
            }
        }
        buffer.append(");").append("\r\n");
        buffer.append(chineseName).append(comment).append(indexed);

        return buffer.toString();
    }


    /**
     * 生成建表语句脚本 MYSQL
     *
     * @param table
     * @param listDetail
     * @return
     * @throws Exception
     */
    public static String creatTableMysql(Table table, List<TableDetail> listDetail) throws Exception {
        StringBuffer buffer = new StringBuffer();
        StringBuffer primarykey = new StringBuffer();
        StringBuffer indexed = new StringBuffer();
        // buffer.append("\r\n").append("DROP TABLE ").append(table.getName()).append(";\r\n");
        buffer.append("\r\n").append("CREATE TABLE ").append(table.getName()).append("\r\n(");
        String chineseName = "";
        if (StringUtils.isNotBlank(table.getChineseName())) {
            chineseName = "comment='" + table.getChineseName() + "';\r\n";
        }
        int i = 0;
        for (TableDetail detail : listDetail) {
            i++;
            if (null == detail || StringUtils.isBlank(detail.getColumnName())) {
                continue;
            }
            buffer.append(detail.getColumnName()).append(" ").append(TableInfoUtil.handleCloum(detail, true, YssDataBaseEnum.MYSQL.getCode()));
            if (StringUtils.isNotBlank(detail.getColumnDefault())) {
                String defaultValue = getColumnDefault(detail.getColumnDefault(), YssDataBaseEnum.MYSQL.getCode());
                if (StringUtils.isNotBlank(defaultValue)) {
                    buffer.append(" DEFAULT  ").append(defaultValue);
                }
            }
            if (2 == detail.getColumnNull()) {
                buffer.append("  NOT NULL");
            }
            if (StringUtils.isNotBlank(detail.getColumnChineseName())) {
                buffer.append("  COMMENT ").append("'").append(detail.getColumnChineseName()).append("'");
            }
            //主键
            if (1 == detail.getPrimaryKey()) {
                primarykey.append(detail.getColumnName()).append(",");
            }
            if (i == listDetail.size() && primarykey.length() < 2) {  //最后一条，表又没有主键的时候，不拼逗号
                buffer.append("\r\n");
            } else {
                buffer.append(",").append("\r\n");
            }
        }
        if (primarykey.length() > 2) {
            buffer.append(" CONSTRAINT ")
                    .append(table.getName())
                    .append(" PRIMARY KEY (" + primarykey.deleteCharAt(primarykey.length() - 1) + ")")
                    .append("\r\n");
        }

        // 索引
        List<TableIndexed> indexedList = table.getListIndexed();
        if (!CollectionUtils.isEmpty(indexedList)) {
            Map<String, List<String>> indexedMap = new HashMap<>();
            for (TableIndexed tableIndexed : indexedList) {
                if (null == tableIndexed) {
                    continue;
                }
                List<String> list = null;
                if (indexedMap.containsKey(tableIndexed.getIndexedName())) {
                    list = indexedMap.get(tableIndexed.getIndexedName());
                    list.add(tableIndexed.getIndexedColumnName());
                } else {
                    list = new ArrayList<>();
                    list.add(tableIndexed.getIndexedColumnName());
                }
                indexedMap.put(tableIndexed.getIndexedName(), list);
            }
            if (!CollectionUtils.isEmpty(indexedMap)) {
                for (Map.Entry<String, List<String>> entry : indexedMap.entrySet()) {
                    if (StringUtils.isBlank(entry.getKey())) {
                        continue;
                    }
                    List<String> list = entry.getValue();
                    if (CollectionUtils.isEmpty(list)) {
                        continue;
                    }
                    indexed.append(" CREATE INDEX " + entry.getKey()).append(" ON ").append(table.getName()).append("(");
                    StringBuffer indexedCol = new StringBuffer();
                    for (String columnName : list) {
                        indexedCol.append(columnName).append(",");
                    }
                    indexed.append(indexedCol.deleteCharAt(indexedCol.length() - 1)).append(")").append(";").append("\r\n");
                }
            }
        }
        buffer.append(")").append(chineseName).append(indexed);

        return buffer.toString();
    }

    /**
     * 获取字段的默认值
     *
     * @return
     */
    public static String getColumnDefault(String columnDefault, String ftype) {
        if (StringUtils.isBlank(columnDefault) || StringUtils.isBlank(ftype)) {
            return null;
        }
        List<TableColumnDefaultVO> list = JSONArray.parseArray(columnDefault, TableColumnDefaultVO.class);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        for (TableColumnDefaultVO vo : list) {
            if (null == vo || StringUtils.isBlank(vo.getFtype())) {
                continue;
            }
            if ("COMMON".equals(vo.getFtype())) {
                return vo.getFvalue();
            }
            if (ftype.equals(vo.getFtype())) {
                return vo.getFvalue();
            }
        }
        return null;
    }


}
